package cfca.seal.web.tag.base;

import cfca.seal.util.FreemarkerUtil;
import cfca.seal.util.StringUtil;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public abstract class TemplateDirectiveModelBase
  implements TemplateDirectiveModel
{
  public String elementId;
  public String elementName;
  public String elementValue;
  public String className;
  public String onChange;
  public String exclude;
  public static Logger log = LoggerFactory.getLogger("web_sys");

  public boolean hasBody = false;
  public HttpServletRequest request;

  private void init(Map paramsMap)
    throws TemplateModelException
  {
    this.elementId = StringUtil.trim(FreemarkerUtil.castDirectiveParamToString((TemplateModel)paramsMap.get("elementId")));
    this.elementName = StringUtil.trim(FreemarkerUtil.castDirectiveParamToString((TemplateModel)paramsMap.get("elementName")));
    this.elementValue = StringUtil.trim(FreemarkerUtil.castDirectiveParamToString((TemplateModel)paramsMap.get("elementValue")));
    this.className = StringUtil.trim(FreemarkerUtil.castDirectiveParamToString((TemplateModel)paramsMap.get("className")));
    this.onChange = StringUtil.trim(FreemarkerUtil.castDirectiveParamToString((TemplateModel)paramsMap.get("onChange")));
    this.exclude = StringUtil.trim(FreemarkerUtil.castDirectiveParamToString((TemplateModel)paramsMap.get("exclude")));

    ServletRequestAttributes attr = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
    this.request = attr.getRequest();
  }

  public void execute(Environment env, Map paramsMap, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    init(paramsMap);
    String result = process(env, paramsMap, loopVars, body);
    Writer out = env.getOut();
    out.write(result);
    if (this.hasBody)
      body.render(out);
  }

  public abstract String process(Environment paramEnvironment, Map paramMap, TemplateModel[] paramArrayOfTemplateModel, TemplateDirectiveBody paramTemplateDirectiveBody)
    throws TemplateException, IOException;
}